{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/69_cartoee_vector.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "**Plotting Earth Engine vector data with cartoee**\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) and [cartopy](https://scitools.org.uk/cartopy/docs/latest/installing.html#installing) if needed. Keep in mind that cartopy can be challenging to install. If you are unable to install cartopy on your computer, you can try Google Colab with this the [notebook example](https://colab.research.google.com/github/gee-community/geemap/blob/master/docs/notebooks/69_cartoee_vector.ipynb). \n",
    "\n",
    "See below the commands to install cartopy and geemap using conda/mamba:\n",
    "\n",
    "```\n",
    "conda create -n carto python=3.8\n",
    "conda activate carto\n",
    "conda install cartopy scipy -c conda-forge\n",
    "conda install geemap -c conda-forge\n",
    "jupyter notebook\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install cartopy scipy\n",
    "# !pip install geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "from geemap import cartoee\n",
    "from geemap.datasets import DATA\n",
    "import geemap.colormaps as cmap\n",
    "import cartopy.crs as ccrs\n",
    "\n",
    "%pylab inline"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "## Plot a simple vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "features = ee.FeatureCollection(DATA.users_giswqs_public_countries)\n",
    "\n",
    "style = {\"color\": \"000000ff\", \"width\": 1, \"lineType\": \"solid\", \"fillColor\": \"0000ff40\"}\n",
    "\n",
    "Map.addLayer(features.style(**style), {}, \"Polygons\")\n",
    "Map.setCenter(-14.77, 34.70, 2)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# specify region to focus on\n",
    "bbox = [180, -88, -180, 88]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(15, 10))\n",
    "\n",
    "# plot the result with cartoee using a PlateCarre projection (default)\n",
    "ax = cartoee.get_map(features, region=bbox, style=style)\n",
    "ax.set_title(label=\"Countries\", fontsize=15)\n",
    "cartoee.add_gridlines(ax, interval=30)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/RTFGotE.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(15, 10))\n",
    "\n",
    "projection = ccrs.EqualEarth(central_longitude=-180)\n",
    "ax = cartoee.get_map(features, region=bbox, proj=projection, style=style)\n",
    "ax.set_title(label=\"Countries\", fontsize=15)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/GagRINK.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "## Plot a styled vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "features = ee.FeatureCollection(DATA.users_giswqs_public_countries)\n",
    "\n",
    "palette = cmap.palettes.gist_earth\n",
    "features_styled = geemap.vector_styling(features, column=\"name\", palette=palette)\n",
    "\n",
    "Map.add_styled_vector(features, column=\"name\", palette=palette, layer_name=\"Polygon\")\n",
    "Map.setCenter(-14.77, 34.70, 2)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "bbox = [180, -88, -180, 88]\n",
    "fig = plt.figure(figsize=(15, 10))\n",
    "\n",
    "ax = cartoee.get_map(features_styled, region=bbox)\n",
    "ax.set_title(label=\"Countries\", fontsize=15)\n",
    "cartoee.add_gridlines(ax, interval=30)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/reecFZo.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(15, 10))\n",
    "\n",
    "projection = ccrs.EqualEarth(central_longitude=-180)\n",
    "ax = cartoee.get_map(features_styled, region=bbox, proj=projection)\n",
    "ax.set_title(label=\"Countries\", fontsize=15)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/uW9p8vS.png)"
   ]
  }
 ],
 "metadata": {
  "kernel_info": {
   "name": "python3"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
